package com.yanqu.road.server.manger.activity.qqprivilege;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.qqprivilege.QQPrivilegeUserData;
import com.yanqu.road.entity.activity.suitang.config.*;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.activity.suitang.SuiTangMapLogic;
import com.yanqu.road.logic.bussiness.activity.SuiTangActivityBusiness;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.qqprivilege.QQPrivilegeActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.suitang.SuiTangModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class QQPrivilegeMgr extends TempMgr {

    // 活动信息
    private static ActivityInfo activityInfo;

    // 每日登陆奖励
    private static String dailySignReward;

    // QQ游戏大厅新注册账号奖励
    private static String accountEnrollReward;

    // appId - register注册时间戳
    private static Map<String, Long> appIdMap = new ConcurrentHashMap<>();


    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    /**
     * 重新加载活动
     */
    public static void reloadActivityData() {
        getLogger().info("reload QQPrivilegeActivity start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.QQPrivilege.getValue());
        if (list.isEmpty()) {
            activityInfo = null;
            getLogger().info("reload QQPrivilegeActivity end, no activity in show time.");
            return;
        }

        loadConfigFromDb(list.get(0));
        activityInfo = list.get(0);

        getLogger().info("reload QQPrivilegeActivity end, activityId {}.", activityInfo.getActivityId());

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(QQPrivilegeActivityModule.class).activityLogin();
        }
    }

    /**
     * 数据库加载配置
     */
    private static void loadConfigFromDb(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return;
        }
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        if (activityConfigMap == null) {
            return;
        }
        dailySignReward = activityConfigMap.get(eActivityConfigType.QQ_PRIVILEGE_DAILY_SIGN_REWARD.getName()).getValue();
        accountEnrollReward = activityConfigMap.get(eActivityConfigType.QQ_PRIVILEGE_ACCOUNT_ENROLL_REWARD.getName()).getValue();
        List<String> appList = activityConfigMap.get(eActivityConfigType.QQ_PRIVILEGE_APP_ID.getName()).getStringListValue("\\|");
        List<Long> registerList = activityConfigMap.get(eActivityConfigType.QQ_PRIVILEGE_REGISTER_TIME.getName()).getLongListValue("\\|");
        if (appList == null || appList.isEmpty() || registerList == null || registerList.isEmpty() || appList.size() != registerList.size()) {
            return;
        }
        for (int i = 0; i < appList.size(); i++) {
            appIdMap.put(appList.get(i), registerList.get(i));
        }
    }

    /*
     * isAble 是否有资格参与活动
     */
    public static boolean isAble(String appId, int ChannelId) {
        return appIdMap != null && appIdMap.containsKey(appId) && ChannelId == eChannelType.Mobile_DY.getValue();
    }

    /*
     * 是否已领取注册奖励
     */
    public static boolean isGotRegisterReward(QQPrivilegeUserData userData, GamePlayer player) {
        if (userData == null) {
            return true;
        }
        if (userData.getGotRegisterReward() == 1) {
            return true;
        }
        UserInfo userInfo = player.getUserInfo();
        if (userInfo == null) {
            return true;
        }
        if (!appIdMap.containsKey(player.getAppId())) {
            return true;
        }
        return userInfo.getRegisterTime() >= appIdMap.get(player.getAppId());
    }

    public static String getDailySignReward() {
        return dailySignReward;
    }

    public static String getAccountEnrollReward() {
        return accountEnrollReward;
    }
}
